﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Galaktika.BI.Silverlight.ClientApp.Items
{
    using Galaktika.BI.Silverlight.Controls;
    using Galaktika.BI.Silverlight.ClientApp.Services;

    public class TabPage : TabItem
    {
        TabHeader m_Header;
        private IndicatorLoading indicatorSelceted;
        private IndicatorLoading indicatorUnselected;
        public TabPage()
        {       
            base.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            base.VerticalContentAlignment = VerticalAlignment.Stretch;
            base.Style = App.Current.Resources["TabControlMainItem"] as Style;
            base.Header = m_Header = new TabHeader();           
            m_Header.QueryClose += new CancelEventHandler(header_QueryClose);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            indicatorSelceted = base.GetTemplateChild("indicatorSelected") as IndicatorLoading;
            indicatorUnselected = base.GetTemplateChild("indicatorUnselected") as IndicatorLoading;
        }
       

        protected override void OnSelected(RoutedEventArgs e)
        {
            base.OnSelected(e);

            m_Header.btnClose.IsChecked = true;
        }

        protected override void OnUnselected(RoutedEventArgs e)
        {
            base.OnUnselected(e);

            m_Header.btnClose.IsChecked = false;
        }
    
        void header_QueryClose(object sender, CancelEventArgs e)
        {
            this.OnQueryClose(e);
        }

        public string Text
        {
            get
            {
                return m_Header.Text;
            }
            set
            {
                m_Header.Text = value;
            }
        }

        public string Hint
        {
            get
            {
                StyledToolTip tp = ToolTipService.GetToolTip(m_Header) as StyledToolTip;
                if (tp != null)
                {
                    return tp.Content as string;
                }

                return null;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    ToolTipService.SetToolTip(m_Header, new StyledToolTip() { Content = value });
                }
            }
        }

        public bool CanClose
        {
            get
            {
                return m_Header.CanClose;
            }
            set
            {
                m_Header.CanClose = value;
            }
        }

        public Visibility IndicatorLoadingVisibility
        {
            get
            {
                return indicatorSelceted == null ? System.Windows.Visibility.Collapsed : indicatorSelceted.Visibility;
            }
            set
            {
                if (this.IndicatorLoadingVisibility != value)
                {
                    this.ClearError();
                    if (indicatorSelceted != null)
                    {
                        if (value == System.Windows.Visibility.Visible)
                        {
                            m_Header.btnClose.IsEnabled = false;
                        }
                        else
                        {
                            m_Header.btnClose.IsEnabled = true;
                        }
                        indicatorSelceted.Visibility = value;
                        indicatorUnselected.Visibility = value;
                    }

                    FormView form = null;
                    ITabPageFormResolver formResolver = App.CurrentApp.Root.TabPageFormResolver;
                    if (formResolver != null)
                    {
                        form = formResolver.GetForm(this);
                    }
                    if (form != null)
                    {
                        if (value == System.Windows.Visibility.Visible)
                        {
                            form.Cursor = Cursors.Wait;
                            form.SwitchToBusyState();
                        }
                        else
                        {
                            form.Cursor = Cursors.Arrow;
                            form.SwitchToFreeState();
                        }
                    }
                }
            }
        }

        private Image m_ErrorImage;
        private Image ErrorImage
        {
            get
            {
                if (m_ErrorImage == null)
                {
                    m_ErrorImage = new Image()
                    {
                        Source = ClientResources.Images.Error16,
                    };
                }

                return m_ErrorImage;
            }
        }

        public void SetError(System.Text.StringBuilder errors)
        {
            if (indicatorSelceted != null)
            {
                this.ClearError();
                var parent = VisualTreeHelper.GetParent(indicatorSelceted) as Grid;
                if (parent != null)
                {
                    parent.Children.Add(this.ErrorImage);

                    if (errors != null && errors.Length != 0)
                    {
                        ToolTipService.SetToolTip(this.ErrorImage, new StyledToolTip() { Content = errors });
                    }
                    else
                    {
                        ToolTipService.SetToolTip(this.ErrorImage, null);
                    }
                }
            }

        }

        public void ClearError()
        {
            if (indicatorSelceted != null)
            {
                if (m_ErrorImage != null)
                {
                    var parent = VisualTreeHelper.GetParent(indicatorSelceted) as Grid;
                    if (parent != null)
                    {
                        parent.Children.Remove(m_ErrorImage);
                    }
                }
            }
        }

        #region Events
        public event CancelEventHandler QueryClose;
        protected void OnQueryClose(CancelEventArgs e)
        {
            if (this.QueryClose != null)
            {
                this.QueryClose(this, e);
            }
        }
        #endregion
    }
}
